﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using Ivi.ConfigServer.Interop;
using AIRO.Devices.Solenoid;
using AIRO.Devices.Ccd;

namespace AIRO.LumSerial
{
    public partial class FrmHwConfig : Form
    {
        FrmMain _fmMain;
        Ivi.ConfigServer.Interop.IviConfigStore cs;
        public FrmHwConfig()
        {
            InitializeComponent();
        }


        public FrmHwConfig(FrmMain fm)
            : this()
        {
            _fmMain = fm;

            cs = new IviConfigStoreClass();
            string location;
            if (cs.ProcessDefaultLocation != "")
            {
                location = cs.ProcessDefaultLocation;
            }
            else if (cs.MasterLocation != "")
            {
                location = cs.MasterLocation;
            }
            else
            {
                throw new Exception("Process Default Location and Master Location are null strings!");
            }
            cs.Deserialize(location);            
        }


        private void btnOk_Click(object sender, EventArgs e)
        {         

            
            AIRO.LumSerial.Properties.Settings.Default.PSLogicalName = cbxPSLogicalName.Text;
            
            AIRO.LumSerial.Properties.Settings.Default.CcdLogicalName = cbxCcdLogicalName.Text;
            AIRO.LumSerial.Properties.Settings.Default.Save();
            this.Hide();
            _fmMain.Activate();

        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
            _fmMain.Activate();
        }

        private void fmHardwareConfig_Load(object sender, EventArgs e)
        {

            string defaultPS = AIRO.LumSerial.Properties.Settings.Default.PSLogicalName;

            string defaultCcd = AIRO.LumSerial.Properties.Settings.Default.CcdLogicalName;

            
            cbxPSLogicalName.Text = "";
            cbxPSLogicalName.Items.Clear();            
            cbxCcdLogicalName.Text = "";
            cbxCcdLogicalName.Items.Clear();

            foreach (IviLogicalName logicalName in cs.LogicalNames)
            {
                if (logicalName.Session != null)
                {
                    IviSoftwareModule softwareModule = logicalName.Session.SoftwareModule;
                    if (softwareModule != null)
                    {
                        
                        
                        
                        string progID = softwareModule.ProgID;
                        Type type = Type.GetTypeFromProgID(progID);
                        if (type != null)
                        {
                            foreach (Type tInterface in type.GetInterfaces())
                            {
                                // Является ли этот интерфейс ISolenoid, т.е. тем что мы ищем
                                if (tInterface == typeof(ISolenoid))
                                {
                                    //Если да, то добавляем logicalName в список опций
                                    cbxPSLogicalName.Items.Add(logicalName.Name);
                                    if (logicalName.Name == defaultPS)
                                        cbxPSLogicalName.SelectedItem = defaultPS;
                                }
                                else if (tInterface == typeof(ICcd))
                                {
                                    cbxCcdLogicalName.Items.Add(logicalName.Name);
                                    if (logicalName.Name == defaultCcd)
                                        cbxCcdLogicalName.SelectedItem = defaultCcd;
                                }
                            }
                        }
                    }
                }
            }
            
            if (cbxPSLogicalName.Items.Count > 0)
            {
            }
            else
            {
                gbPowerSupply.Enabled = false;
            }
            

            if (cbxCcdLogicalName.Items.Count > 0)
            {

            }
            else
            {
                gbCcd.Enabled = false;
                MessageBox.Show("Error! No IVI CCD found", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }



    }
}
